<?php
class Abroadcaster {
	public static function main($funcode) {
		sview::verify(true, true);
		switch ($funcode) {
			case 1:
				self::__docheck();
				break;
			case 2:
				self::__dostatus();
				break;
			case 3:
				self::__dosubmit();
				break;
			case 4:
				self::__dostatistic();
				break;
			case 5:
				self::__docancel();
				break;
			case 6:
				self::__domulti_check();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//开播检查，点击开播的时候使用 5-4-6 替代5-4-1
	private static function __domulti_check() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$where  = array('userid'=>$userid, 'status'=>1);
		$studio = model::load('live/studio')->get_item($where);
		if (!$studio) {
			sview::$view->assign_error(80081, lang::text(80081));
		}
		$isyset = (int)api_studio::isplayset($studio);
		if (!$isyset) {
			sview::$view->assign_error(80083, lang::text(80083));
		}
		$studio['ptocken'] = randme::rand_str(8, 2);
		$studio['rtocken'] = randme::rand_str(8, 2);
		//后台标记的待开启
		$evtid = api_studio::geteid($studio['id'], $userid);
		$app   = stripos($_SERVER['HTTP_USER_AGENT'], 'ios')? 'ios':'android';
		if ($studio['olstatus'] == 0 || $studio['oltools'] != $app) {
			$evtid = api_studio::event_check($evtid, $userid, $studio);
		}
		//促发添加机器人
		api_robots::add($studio['id']);
		$rtmps = api_rtmp::push($app, $studio, $evtid);
		$rtmp  = empty($rtmps)? '':$rtmps[0];
		$field = array('ptocken'=>$studio['ptocken'], 'rtocken'=>$studio['rtocken'], 'isset'=>0, 'isshow'=>1,
			'rtmp'=>$rtmp, 'oltools'=>$app, 'olstatus'=>0, 'lasttime'=>config::$nowtime, 'neartime'=>config::$nowtime);
		model::load('live/studio')->save($studio['id'], $field);
		$dataset = array('eventid'=>(int)$evtid, 'rtmps'=>$rtmps);
		//开播状态IM要广播出去...
		//api_im::notifystatus($studio['id'], $studio['userid'], $studio['rtocken'], 1, $evtid);
		//客户端push提醒
		api_im::notifypushfans($studio);
		sview::$view->assign('datasets', $dataset);
		sview::$view->display();
	}
	
	//开播检查，点击开播的时候使用 5-4-1
	private static function __docheck() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$where  = array('userid'=>$userid, 'status'=>1);
		$studio = model::load('live/studio')->get_item($where);
		if (!$studio) {
			sview::$view->assign_error(80081, lang::text(80081));
		}
		$isyset = (int)api_studio::isplayset($studio);
		if (!$isyset) {
			sview::$view->assign_error(80083, lang::text(80083));
		}
		$studio['ptocken'] = randme::rand_str(8, 2);
		$studio['rtocken'] = randme::rand_str(8, 2);
		//后台标记的待开启
		$evtid = api_studio::geteid($studio['id'], $userid);
		$app   = stripos($_SERVER['HTTP_USER_AGENT'], 'ios')? 'ios':'android';
		if ($studio['olstatus'] == 0 || $studio['oltools'] != $app) {
			$evtid = api_studio::event_check($evtid, $userid, $studio);
		}
		//促发添加机器人
		api_robots::add($studio['id']);
		$rtmp   = api_rtmpuri::push($studio['ptocken'], $app, $studio['id'], $studio['userid'], $evtid);
		$field  = array('ptocken'=>$studio['ptocken'], 'rtocken'=>$studio['rtocken'], 'isset'=>0, 'isshow'=>1,
			'rtmp'=>$rtmp, 'oltools'=>$app, 'olstatus'=>0, 'lasttime'=>config::$nowtime, 'neartime'=>config::$nowtime);
		model::load('live/studio')->save($studio['id'], $field);
		$dataset = array('eventid'=>(int)$evtid, 'rtmp'=>$rtmp);
		//开播状态IM要广播出去...
		//api_im::notifystatus($studio['id'], $studio['userid'], $studio['rtocken'], 1, $evtid);
		//客户端push提醒
		api_im::notifypushfans($studio);
		sview::$view->assign('datasets', $dataset);
		sview::$view->display();
	}
	
	//会员开播之后状态上报，每1分钟上报一次，作为心跳说明在直播 5-4-2
	private static function __dostatus() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$evtid  = (int)sview::$ctrl['eventid'];
		$status = (int)sview::$ctrl['status'];
		$across = (int)sview::$ctrl['across'];
		$sorm   = model::load('live/studio');
		$eorm   = model::load('live/stuevent')->set_devision($userid);
		$evnt   = $eorm->get_one($evtid);
		if (!$evnt || $evnt['userid'] != $userid) {
			sview::$view->assign_error(80081, lang::text(80081));
		}
		$studio = $sorm->get_one($evnt['studioid']);
		$nevtid = api_studio::geteid($studio['id'], $userid);
		if ($nevtid != $evtid) {
			sview::$view->assign_error(80081, lang::text(80081));
		}
		$olmax  = api_im::getolbyid($evnt['studioid']);
		$field  = array('lasttime'=>config::$nowtime, 'offtime'=>0, 'across'=>$across);
		if ($olmax > $evnt['olmax']) {
			$field['olmax'] = $olmax;
		}
		//直播中的状态上报请求
		if ($status == 1) {
			//如果端不一样的话返回错误
			$app    = stripos($_SERVER['HTTP_USER_AGENT'], 'ios')? 'ios':'android';
			if ($studio['oltools'] != $app) {
				sview::$view->assign_error(80081, lang::text(80081));
			}
			if ($studio['olstatus'] == 0) {//状态变动的话要通知前端
				$sorm->save($studio['id'], array('lasttime'=>config::$nowtime, 'olstatus'=>1));
				//开播状态IM要广播出去...
				api_im::notifystatus($studio['id'], $studio['userid'], $studio['rtocken'], 1, $evtid);
				$user  = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
				api_im::notifystart($user, $studio);
			}
			$eorm->save($evtid, $field);
		} else {
			if ($studio['olstatus'] == 1) {
				$sorm->save($studio['id'], array('lasttime'=>config::$nowtime, 'olstatus'=>0));
				$field['offtime'] = config::$nowtime;
				$eorm->save($evtid, $field);
			}
			//开播状态IM要广播出去...
			api_im::notifystatus($studio['id'], $userid, $studio['rtocken'], 0, 0);
			//促发删除机器人
			api_robots::del($studio['id']);
		}
		sview::$view->display();
	}
	
	//提交完成开播设置 5-4-3
	private static function __dosubmit() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$url    = trim(sview::$ctrl['thumb']);
		if (!$url) {
			sview::$view->assign_error(80117, lang::text(80117));
		}
		$tagids  = trim(sview::$ctrl['tagids']);
		$isfb    = (int)sview::$ctrl['isfb'];
		$airtime = (int)sview::$ctrl['airtime'];
		$title   = trim(sview::$ctrl['title']);
		$nlen    = mb_strlen($title, 'utf-8');
		if ($nlen > 70) {
			sview::$view->assign_error(80050, lang::text(80050));
		}
		if ($nlen > 0) {//验证屏蔽关键词
			$res = api_im::filter(2, $title);
			if ($res && $res['ResultContent'] == 1) {
				$lang = lang::load('filter.php');
				sview::$view->assign_error(80050, lang::replace(80088, $lang['title']));
			}
		}
		$tagids  = explode(',', $tagids);
		$tagids  = array_filter($tagids, 'is_numeric');
		if (!$tagids) {
			sview::$view->assign_error(80051, lang::text(80051));
		}
		$studio  = model::load('live/studio')->get_item(array('userid'=>$userid));
		$isnewer = 0;
		if (!$studio) {
			$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
			if (!$user['emailverify']) {
				sview::$view->assign_error(80114, lang::text(80114));
			}
			$studio = api_studio::open_studio($userid, $user);
			$isnewer= 1;
		} else if ($studio['status'] != 1) {
			sview::$view->assign_error(80115, lang::text(80115));
		}
		//设置标签资料信息
		$fields  = array('thumb'=>$url, 'title'=>$title, 'isset'=>1,
			'mshare'=>$isfb, 'stime'=>config::$nowtime);
		$fields['tags'] = api_studio::setTags($studio, $tagids);
		//预约时间不一致的话要重置之前的预约时间
		if ($airtime > 0) {
			if ($studio['airtime'] > config::$nowtime) {
				api_studio::airNotify($studio, $airtime);
			}
			$fields['airtime'] = $airtime;
		}
		if ($studio && $studio['isshow'] == 0) {
			$fields['isshow'] = ($studio['origin'] == 0)? 2:1;
		}
		//获取标签对象 set到studio当中
		model::load('live/studio')->save($studio['id'], $fields);
		$link = router::get_base_url().router::get_url('live', $studio['studiono']);
		api_fbgraph::clear($link);
		sview::$view->assign_by_ref('isnewer', $isnewer);
		sview::$view->display();
	}
	
	//提交完成开播设置 5-4-4直播结束统计信息
	private static function __dostatistic() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$where  = array('userid'=>$userid, 'status'=>1);
		$studio = model::load('live/studio')->get_item($where);
		if (!$studio) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		$anchor = model::load('live/anchor')->get_one($userid);
		$eid    = isset(sview::$ctrl['eventid'])? sview::$ctrl['eventid']:api_studio::geteid($studio['id'], $userid);
		$orm    = model::load('live/stuevent');
		$event  = $orm->set_devision($userid)->get_one($eid);
		if (!$event) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		if ($event['lasttime'] < 1 || (config::$nowtime - $event['lasttime']) < 60) {
			$event['lasttime'] = config::$nowtime;
		}
		$airtime= $event['lasttime'] - $event['aritime'];
		$coins  = ($anchor['earningcoins'] - $anchor['salarycoins']) - ($event['earing'] - $event['salary']);
		$links  = $anchor['charms'] - $event['like'];
		$orm->get_orm()->where('id', $eid)->update(array('charms'=>$links));
		$viewer = api_charms::getviewer($studio['id'], false);
		$xfans  = $anchor['xfans']  - $event['xfans'];
		$dataset= array('duration'=>$airtime, 'coin'=>$coins, 'likes'=>$links, 'viewer'=>$viewer, 'xfans'=>$xfans);
		sview::$view->assign('datasets', $dataset);
		sview::$view->display();
	}
	
	//提交完成开播设置 5-4-5直播结束统计信息
	private static function __docancel() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$studio = model::load('live/studio')->get_item(array('userid'=>$userid));
		if ($studio && $studio['airtime'] > config::$nowtime) {
			model::load('live/studio')->save($studio['id'], array('airtime'=>0));
			$list = api_studio::userfeed($studio['id']);
			//删除预约的信息
			model::load('live/subscibe')->get_orm()->where('studioid', $studio['id'])->delete();
			model::load('live/subscibe')->save_cache_ver();
			if ($list) {
				lang::load('sitemsg.php');
				$title  = lang::replace(50014);
				$airtime= api_coreutil::timezone($studio['airtime']);
				$user   = model::load('live/userinfo')->set_devision($studio['userid'])->get_one($studio['userid']);
				$user['nickname'] = htmlspecialchars_decode($user['nickname']);
				$msg    = lang::replace(50015, $user['nickname'], $airtime);
				$sql    = 'INSERT INTO vs_msg (id,senderid,dictid,reftype,refid,title,msg,reveiverid,status,stime) VALUES';
				$stime  = config::$nowtime;
				foreach ($list as $item) {
					$arr[] = "(0, 0, 1, 0, '{$item['userid']}','{$title}','{$msg}',{$item['userid']}, 1, {$stime})";
				}
				model::load('live/msg')->get_orm()->get_parser()->execute($sql.implode(',', $arr));
				model::load('live/msg')->save_cache_ver();
			}
		}
		sview::$view->display();
	}
}